package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MenuDao;
import com.itheima.dao.RoleDao;
import com.itheima.dao.UserDao;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import com.itheima.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service(interfaceClass = MenuService.class)
@Transactional
public class MenuServiceImpl implements MenuService {
    @Autowired
    UserDao userDao;
    @Autowired
    RoleDao roleDao;
    @Autowired
    MenuDao menuDao;
    @Override
    public List<Menu> findMenuListByUsername(String username) {
        //根据用户名找到用户id
        User user = userDao.findByUsername(username);
        int userId = user.getId();
        //根据用户id找到所有的角色id
        Set<Role> roleSet = roleDao.findByUserId(userId);
        //创建一个菜单的容器
        Set<Menu> menuSet = new HashSet<>();
        for (Role role : roleSet) {
            Integer roleId = role.getId();
        //根据所有的角色id找到菜单,不同角色可能拥有相同的菜单id,使用set集合去重
            menuSet.addAll(menuDao.findByRoleId(roleId));
        }
        List<Menu> menuList = recursionMenuList(menuSet);
        return menuList;
        /*//封装拿到的所有菜单，根据前端格式传给前端
        List<Menu> parentMenuList = new ArrayList<>();
        for (Menu menu : menuSet) {
            //先将菜单父类和子类分开存放
            Integer parentMenuLevel = menu.getLevel();
            if(parentMenuLevel!=null&&parentMenuLevel==1){
                //加入list
                parentMenuList.add(menu);
            }
        }
        //从set移除,剩下的就是子类和子类的子类
        for (Menu menu : parentMenuList) {
            menuSet.remove(menu);
        }

            for (Menu parentMenu : parentMenuList) {
                List<Menu> childMenuList = new ArrayList<>();
                for (Menu menu : menuSet) {
                    if(menu.getParentMenuId()!=null&&parentMenu.getId() == menu.getParentMenuId()){
                        childMenuList.add(menu);
                    }
                }
                //排序
                Collections.sort(childMenuList);
                parentMenu.setChildren(childMenuList);
            }
        //排序
        Collections.sort(parentMenuList);
        return parentMenuList;*/


    }
    public List<Menu> recursionMenuList(Set<Menu> menuSet) {
        //找到所有的一级菜单
        List<Menu> menuList = new ArrayList<>();
        for (Menu menu : menuSet) {
            if (menu.getLevel()!=null&&menu.getLevel()==1) {
                //找到一级菜单下的子菜单集合的子菜单集合
                findChildren(menu,menuSet);
                menuList.add(menu);
            }
        }
        Collections.sort(menuList);
        return menuList;
    }
    public void findChildren (Menu menu,Set<Menu> menuSet)
    {
        List<Menu> menuChildren = new ArrayList<>();
        for (Menu menu1 : menuSet) {
            if (menu.getId()!=null&&menu.getId()==menu1.getParentMenuId())
            {
                menuChildren.add(menu1);
                findChildren(menu1,menuSet);
            }
        }
        Collections.sort(menuChildren);
        menu.setChildren(menuChildren);
    }


}
